package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.CategoryBiz;
import com.yc.sole.erp.request.CategoryQueryReq;
import com.yc.sole.erp.request.CategoryReq;
import com.yc.sole.erp.response.CategoryResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 类别
 *
 * @author yizuomin
 * @date 2023-12-13
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryBiz categoryBiz;

    /**
     * 新增
     *
     * @param categoryReq
     * @return RestResult
     */
    @PreAuth(value = "erp:category:save", description = "新增分类")
    @PostMapping("/api/erp/category/saveCategory")
    public Result<Void> saveCategory(@RequestBody @Validated(SaveGroup.class) CategoryReq categoryReq) {
        categoryBiz.saveCategory(categoryReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param categoryReq
     * @return RestResult
     */
    @PreAuth(value = "erp:category:update", description = "修改分类")
    @PostMapping("/api/erp/category/updateCategoryById")
    public Result<Void> updateCategoryById(@RequestBody @Validated(UpdateGroup.class) CategoryReq categoryReq) {
        categoryBiz.updateCategoryById(categoryReq);
        return Result.success();
    }

    /**
     * 通过Id查询分类
     *
     * @param categoryId
     * @return RestResult
     */
    @PreAuth(value = "erp:category:select", description = "通过Id查询分类")
    @PostMapping("/api/erp/category/selectCategoryById/{categoryId}")
    public Result<CategoryResp> selectCategoryById(@PathVariable Long categoryId) {
        return Result.data(categoryBiz.selectCategoryById(categoryId));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:category:delete", description = "删除分类")
    @PostMapping("/api/erp/category/deleteCategory")
    public Result<Void> deleteCategory(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        categoryBiz.deleteCategoryByIds(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 分类树
     *
     * @return Result
     */
    @PreAuth(value = "erp:category:select", description = "查询分类")
    @PostMapping("/api/erp/category/selectCategoryTree")
    public Result<List<CategoryResp>> selectCategoryTree(@RequestBody CategoryQueryReq categoryQueryReq) {
        return Result.data(categoryBiz.selectCategoryTree(categoryQueryReq));
    }
}
